<template>
  <div class="page-container">
    <button class="back-button" @click="goToHome">
      <span class="icon-arrow-left"></span>
      返回首页
    </button>
    <h1>任务分配</h1>
    <p>管理工单任务的分配、跟踪与完成情况</p>
    
    <div class="dispatch-card">
      <div class="filter-bar">
        <select v-model="selectedWorkOrder" class="form-control" @change="loadTasks">
          <option value="">选择工单</option>
          <option v-for="order in workOrders" :key="order.id" :value="order.id">
            {{ order.number }} - {{ order.title }}
          </option>
        </select>
        
        <select v-model="taskStatus" class="form-control">
          <option value="">任务状态</option>
          <option value="pending">待分配</option>
          <option value="assigned">已分配</option>
          <option value="inProgress">进行中</option>
          <option value="completed">已完成</option>
          <option value="cancelled">已取消</option>
        </select>
        
        <button class="btn-add" @click="showAddTaskModal = true">+ 添加任务</button>
      </div>
      
      <div class="task-list">
        <div class="task-card" v-for="task in filteredTasks" :key="task.id">
          <div class="task-header">
            <h3>{{ task.title }}</h3>
            <span :class="['status-tag', task.status]">{{ task.statusLabel }}</span>
          </div>
          
          <div class="task-details">
            <p><strong>描述:</strong> {{ task.description }}</p>
            <p><strong>优先级:</strong> <span :class="['priority-tag', task.priority]">{{ task.priorityLabel }}</span></p>
            <p><strong>截止日期:</strong> {{ task.dueDate }}</p>
            <p><strong>负责人:</strong> {{ task.assigneeName || '未分配' }}</p>
          </div>
          
          <div class="task-actions">
            <button class="btn-edit" @click="editTask(task)">编辑</button>
            <button class="btn-assign" @click="assignTaskToMember(task)" 
              v-if="task.status === 'pending' || task.status === 'assigned'">
              {{ task.assigneeId ? '重新分配' : '分配' }}
            </button>
            <button class="btn-complete" @click="markAsComplete(task)" 
              v-if="task.status !== 'completed' && task.status !== 'cancelled'">
              标记完成
            </button>
          </div>
        </div>
        
        <div v-if="filteredTasks.length === 0" class="empty-state">
          暂无任务数据，请先选择工单或添加任务
        </div>
      </div>
    </div>
    
    <!-- 分配任务弹窗 -->
    <div v-if="showAssignModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ currentTask ? '分配任务: ' + currentTask.title : '分配任务' }}</h3>
          <button class="close-btn" @click="showAssignModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>分配给:</label>
            <select v-model="currentTask.assigneeId" class="form-control">
              <option value="">选择负责人</option>
              <option v-for="member in teamMembers" :key="member.id" :value="member.id">
                {{ member.name }}
              </option>
            </select>
          </div>
          <div class="form-group">
            <label>分配备注:</label>
            <textarea v-model="assignmentRemark" class="form-control" rows="3"></textarea>
          </div>
          <div class="form-group">
            <label>预计完成时间:</label>
            <input type="date" v-model="currentTask.dueDate" class="form-control">
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn-cancel" @click="showAssignModal = false">取消</button>
          <button class="btn-confirm" @click="confirmTaskAssignment">确认分配</button>
        </div>
      </div>
    </div>
    
    <!-- 添加/编辑任务弹窗 -->
    <div v-if="showAddTaskModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ isEditingTask ? '编辑任务' : '添加新任务' }}</h3>
          <button class="close-btn" @click="showAddTaskModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>任务名称:</label>
            <input type="text" v-model="newTask.title" class="form-control" placeholder="输入任务名称">
          </div>
          <div class="form-group">
            <label>任务描述:</label>
            <textarea v-model="newTask.description" class="form-control" rows="3" placeholder="输入任务描述"></textarea>
          </div>
          <div class="form-group">
            <label>优先级:</label>
            <select v-model="newTask.priority" class="form-control">
              <option value="low">低</option>
              <option value="medium">中</option>
              <option value="high">高</option>
              <option value="urgent">紧急</option>
            </select>
          </div>
          <div class="form-group">
            <label>截止日期:</label>
            <input type="date" v-model="newTask.dueDate" class="form-control">
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn-cancel" @click="showAddTaskModal = false">取消</button>
          <button class="btn-confirm" @click="saveTask">保存任务</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TaskDispatch',
  data() {
    return {
      selectedWorkOrder: '',
      taskStatus: '',
      workOrders: [
        { id: 1, number: 'WO-2023-001', title: '办公室网络升级' },
        { id: 2, number: 'WO-2023-002', title: '生产车间设备维护' },
        { id: 3, number: 'WO-2023-003', title: '仓库整理与盘点' }
      ],
      tasks: [],
      teamMembers: [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' },
        { id: 3, name: '王五' },
        { id: 4, name: '赵六' }
      ],
      showAssignModal: false,
      showAddTaskModal: false,
      currentTask: null,
      assignmentRemark: '',
      newTask: {
        title: '',
        description: '',
        priority: 'medium',
        dueDate: '',
        workOrderId: ''
      },
      isEditingTask: false
    }
  },
  computed: {
    filteredTasks() {
      return this.tasks.filter(task => {
        if (this.taskStatus && task.status !== this.taskStatus) return false;
        return true;
      });
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    loadTasks() {
      if (!this.selectedWorkOrder) {
        this.tasks = [];
        return;
      }
      
      // 模拟加载任务数据
      this.tasks = [
        { 
          id: 1, 
          title: '检查网络设备', 
          description: '检查所有路由器、交换机运行状态',
          status: 'pending',
          statusLabel: '待分配',
          priority: 'high',
          priorityLabel: '高',
          dueDate: '2023-11-30',
          workOrderId: this.selectedWorkOrder,
          assigneeId: null,
          assigneeName: ''
        },
        { 
          id: 2, 
          title: '更换老化网线', 
          description: '更换办公室内老化的网线',
          status: 'assigned',
          statusLabel: '已分配',
          priority: 'medium',
          priorityLabel: '中',
          dueDate: '2023-12-02',
          workOrderId: this.selectedWorkOrder,
          assigneeId: 2,
          assigneeName: '李四'
        },
        { 
          id: 3, 
          title: '配置新路由器', 
          description: '配置并安装新的主路由器',
          status: 'inProgress',
          statusLabel: '进行中',
          priority: 'urgent',
          priorityLabel: '紧急',
          dueDate: '2023-11-28',
          workOrderId: this.selectedWorkOrder,
          assigneeId: 1,
          assigneeName: '张三'
        }
      ];
    },
    assignTaskToMember(task) {
      this.currentTask = { ...task };
      this.assignmentRemark = '';
      this.showAssignModal = true;
    },
    confirmTaskAssignment() {
      if (!this.currentTask.assigneeId) {
        alert('请选择负责人');
        return;
      }
      
      // 找到成员名称
      const member = this.teamMembers.find(m => m.id === this.currentTask.assigneeId);
      
      // 更新任务状态和负责人
      const taskIndex = this.tasks.findIndex(t => t.id === this.currentTask.id);
      if (taskIndex !== -1) {
        this.tasks[taskIndex].assigneeId = this.currentTask.assigneeId;
        this.tasks[taskIndex].assigneeName = member.name;
        this.tasks[taskIndex].status = 'assigned';
        this.tasks[taskIndex].statusLabel = '已分配';
        this.tasks[taskIndex].dueDate = this.currentTask.dueDate;
      }
      
      this.showAssignModal = false;
      this.$message.success('任务分配成功');
    },
    markAsComplete(task) {
      const taskIndex = this.tasks.findIndex(t => t.id === task.id);
      if (taskIndex !== -1) {
        this.tasks[taskIndex].status = 'completed';
        this.tasks[taskIndex].statusLabel = '已完成';
      }
      this.$message.success('任务已标记为完成');
    },
    editTask(task) {
      this.isEditingTask = true;
      this.newTask = { ...task };
      this.showAddTaskModal = true;
    },
    saveTask() {
      if (!this.newTask.title) {
        alert('请输入任务名称');
        return;
      }
      
      if (this.isEditingTask) {
        // 编辑现有任务
        const taskIndex = this.tasks.findIndex(t => t.id === this.newTask.id);
        if (taskIndex !== -1) {
          this.tasks[taskIndex] = { ...this.newTask };
        }
        this.$message.success('任务更新成功');
      } else {
        // 添加新任务
        if (!this.selectedWorkOrder) {
          alert('请先选择工单');
          return;
        }
        
        const newTask = {
          ...this.newTask,
          id: Date.now(),
          workOrderId: this.selectedWorkOrder,
          status: 'pending',
          statusLabel: '待分配',
          priorityLabel: this.getPriorityLabel(this.newTask.priority),
          assigneeId: null,
          assigneeName: ''
        };
        
        this.tasks.push(newTask);
        this.$message.success('任务添加成功');
      }
      
      this.showAddTaskModal = false;
      this.isEditingTask = false;
      this.newTask = {
        title: '',
        description: '',
        priority: 'medium',
        dueDate: '',
        workOrderId: ''
      };
    },
    getPriorityLabel(priority) {
      const labels = {
        low: '低',
        medium: '中',
        high: '高',
        urgent: '紧急'
      };
      return labels[priority] || '中';
    }
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  margin-bottom: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.dispatch-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  padding: 20px;
  margin-top: 15px;
}

.filter-bar {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
  align-items: center;
  flex-wrap: wrap;
}

.form-control {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  width: 220px;
}

.btn-add {
  margin-left: auto;
  background: #2ecc71;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s;
  display: flex;
  align-items: center;
  gap: 5px;
}

.btn-add:hover {
  background: #27ae60;
}

.task-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 15px;
  margin-top: 20px;
}

.task-card {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 15px;
  transition: transform 0.3s, box-shadow 0.3s;
}

.task-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.task-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
}

.task-header h3 {
  margin: 0;
  font-size: 16px;
}

.task-details {
  margin-bottom: 15px;
  font-size: 14px;
}

.task-details p {
  margin: 5px 0;
}

.task-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.btn-edit, .btn-assign, .btn-complete {
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  border: none;
  transition: background 0.3s;
}

.btn-edit {
  background: #f1c40f;
  color: #333;
}

.btn-edit:hover {
  background: #d4ac0d;
}

.btn-assign {
  background: #3498db;
  color: white;
}

.btn-assign:hover {
  background: #2980b9;
}

.btn-complete {
  background: #2ecc71;
  color: white;
}

.btn-complete:hover {
  background: #27ae60;
}

.status-tag {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  color: white;
}

.status-tag.pending {
  background-color: #95a5a6;
}

.status-tag.assigned {
  background-color: #3498db;
}

.status-tag.inProgress {
  background-color: #f39c12;
}

.status-tag.completed {
  background-color: #2ecc71;
}

.status-tag.cancelled {
  background-color: #e74c3c;
}

.priority-tag {
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 12px;
  color: white;
}

.priority-tag.low {
  background-color: #95a5a6;
}

.priority-tag.medium {
  background-color: #3498db;
}

.priority-tag.high {
  background-color: #f39c12;
}

.priority-tag.urgent {
  background-color: #e74c3c;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #7f8c8d;
  background-color: #f9f9f9;
  border-radius: 8px;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #7f8c8d;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.btn-cancel {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-confirm {
  padding: 8px 16px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn-confirm:hover {
  background: #2980b9;
}

@media (max-width: 768px) {
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }
  
  .form-control {
    width: 100%;
  }
  
  .btn-add {
    margin-left: 0;
    width: 100%;
    justify-content: center;
  }
  
  .task-list {
    grid-template-columns: 1fr;
  }
}
</style>